//
//  MineHomeViewController.m
//  魔兽大数据
//
//  Created by 王右 on 2017/12/6.
//  Copyright © 2017年 王右. All rights reserved.
//

#import "MineHomeViewController.h"
#import "BigDataHeader.h"
#import "MineHomeTableViewCell.h"
#import "ServerListViewController.h"
#import "PopulationCensusViewController.h"
#import "GoldCoinPriceViewController.h"
#import "ServerModel.h"

@interface MineHomeViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *baseTableView;
@property (nonatomic, strong) NSMutableArray *titleArr;
@end

@implementation MineHomeViewController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [_baseTableView reloadData];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    _baseTableView.estimatedRowHeight = 44;
    _titleArr = @[@[@"我的服务器",@"金币价格",@"时光徽章"],@[@"人口普查",@"设置",@"关于我们"]].mutableCopy;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [_titleArr[section] count];
}

- (NSInteger )numberOfSectionsInTableView:(UITableView *)tableView{
    return _titleArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MineHomeTableViewCell *cell = (MineHomeTableViewCell *)[tableView cellWithCellNibName:@"MineHomeTableViewCell"];
    cell.titleLabel.text = _titleArr[indexPath.section][indexPath.row];
    cell.iconImageView.image = [UIImage imageNamed:_titleArr[indexPath.section][indexPath.row]];
    cell.desLabel.text = [self returnStringWithIndex:indexPath];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSInteger section = indexPath.section;
    NSInteger row = indexPath.row;
    
    if (section==0) {
        switch (row) {
            case 0:{
                ServerListViewController *vc = [ServerListViewController new];
                [self.navigationController pushViewController:vc animated:YES];
            }
                break;
            case 1:{
                GoldCoinPriceViewController *vc = [GoldCoinPriceViewController new];
                [self.navigationController pushViewController:vc animated:YES];
            }
                break;
            case 2:{
                
            }
                break;
            default:
                break;
        }
    }else{
        switch (row) {
            case 0:{
                PopulationCensusViewController *vc = [PopulationCensusViewController new];
                [self.navigationController pushViewController:vc animated:YES];
            }
                break;
            case 1:{
                
            }
                break;
            case 2:{
                
            }
            default:
                break;
        }
    }

}

- (NSString *)returnStringWithIndex:(NSIndexPath *)index{
    NSInteger section = index.section;
    if (section>0) {
        return @"";
    }
    switch (index.row) {
        case 0:{
            ServerModel *model = [[CCCache defaultManager] objectForKey:@"myHomeServer"];
            return model.targetServerName;
        }
            break;
        case 1:{
            
            NSNumber *averagePrice = [[CCCache defaultManager] objectForKey:@"serverGoldPriceAverage"];
            if (averagePrice) {
                return [NSString stringWithFormat:@"均价:%@元/万",averagePrice];
            }else{
                return @"暂无价格数据";
            }
            break;
        }
        case 2:{
            return @"最新价格344440";
        }
        default:
            return @"";
            break;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
